package com.jeesite.modules.form.web;

import java.util.List;

import com.jeesite.common.utils.OrgUtil;
import com.jeesite.modules.form.entity.qo.FormQO;
import com.jeesite.modules.form.entity.vo.FormVO;
import com.jeesite.modules.formcategory.service.FormCategoryService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.form.entity.Form;
import com.jeesite.modules.form.service.FormService;

/**
 * 表单配置Controller
 * @author 码脑洞
 * @version 2025-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/form/form")
public class FormController extends BaseController {

	@Autowired
	private FormService formService;
	@Autowired
	private FormCategoryService formCategoryService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Form get(Long autoId, boolean isNewRecord) {
		return formService.get(autoId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("form:form:view")
	@RequestMapping(value = {"list", ""})
	public String list(Form form, Model model) {
		model.addAttribute("form", form);
		return "modules/form/formList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("form:form:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<FormVO> listData(FormQO formQO, HttpServletRequest request, HttpServletResponse response) {
		formQO.setPage(new Page<>(request, response));
		Page<FormVO> page = formService.findFormPage(formQO);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("form:form:view")
	@RequestMapping(value = "form")
	public String form(Form form, Model model) {
		model.addAttribute("formCategoryList", formCategoryService.selectFormCategoryList());
		if (OrgUtil.isValid()){
			form.setOrgId(OrgUtil.getOrgId());
			form.setOrgCode(OrgUtil.getOrgCode());
			form.setOrgName(OrgUtil.getOrgName());
		}
		model.addAttribute("form", form);
		return "modules/form/formForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("form:form:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Form form) {
		formService.save(form);
		return renderResult(Global.TRUE, text("保存表单配置成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("form:form:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Form form) {
		formService.delete(form);
		return renderResult(Global.TRUE, text("删除表单配置成功！"));
	}
	
}